<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <span>用户列表</span>
        <router-link :to="{path:'/user/adduser'}">
          <el-button style="float: right;padding: 3px 50px 3px" type="primary">新添用户</el-button>
        </router-link>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          type="index"
          :index="indexMethod"
          label="序号"
          width="180">
        </el-table-column>
        <el-table-column
          label="用户名"
          width="180">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>用户名: {{ scope.row.username }}</p>
              <p>账户名: {{ scope.row.account }}</p>
              <div v-if="scope.row.authority===1">
                <p>权限: {{ "管理员" }}</p>
              </div>
              <div v-if="scope.row.authority===99">
                <p>权限: {{ "普通用户" }}</p>
              </div>
              <p>生日: {{ scope.row.birthday }}</p>
              <div v-if="scope.row.sex===1">
                <p>性别: {{ "男" }}</p>
              </div>
              <div v-if="scope.row.sex===2">
                <p>性别: {{ "女" }}</p>
              </div>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.username }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <router-link :to="{path: '/user/changepassword',
            query:{account:scope.row.account}}" >
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">修改密码</el-button>
            </router-link>
            <el-button
              size="mini"
              type="danger"
              @click="open(scope.row.account)">注销</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

  </div>


</template>

<script>
  export default {
    data() {
      return {
        tableData: []
      }
    },
    mounted() {
      this.init();
    },
    methods: {
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
        console.log(index, row);
      },
      indexMethod(index) {
        return index+1 ;
      },
      init:function() {
        this.$axios({
          headers:{
            "Content-Type": "application/json"
          },
          methods: "post",
          url:"http://localhost:8080/api/user/userlist",
          data:{}
        }).then((res)=>{
          console.log(res.data);
          if(res.data.resultCode==="0"){
            this.tableData= res.data.userList;

          }
        }).catch((error)=>{
          console.log(error);
        })

      },
      open(account) {
        this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          width:80,
          center:true
        }).then(() => {
          this.deleteuser(account)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      deleteuser:function(account) {
        const dd={account:account}
        // console.log(id)
        this.$axios({
          headers:{
            "Content-Type":"application/json"
          },
          method: "post",
          url:"http://localhost:8080/api/user/deleteuser",
          data:{
            account:dd.account
          }
        }).then((res)=>{
          if(res.data.resultCode==="0"){
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.init()
          }
        }).catch((error)=>{
          console.log(error);
        })
      }
    }
  }
</script>

<style scoped>

</style>
